package ru.spb.osll.myfit.client.manager;

import ru.spb.osll.myfit.client.service.PageExecutingService;
import ru.spb.osll.myfit.client.service.PageExecutingServiceAsync;
import ru.spb.osll.myfit.client.widget.PageExecutingListenerImpl;
import ru.spb.osll.myfit.client.widget.PageWidget;
import ru.spb.osll.myfit.client.widget.ProgressBar;
import ru.spb.osll.myfit.common.PageExecutingResult;
import ru.spb.osll.myfit.common.PageExecutingResult.PageExecutingState;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageExecutingManager {

  private boolean startIndicator = false;
  
  private ProgressBar m_progressBar;
  
  private final PageExecutingServiceAsync m_pageExecutingService = GWT.create(PageExecutingService.class);
  
  private PageExecutingListenerImpl executingListener;
  
 
  public PageExecutingManager() {
  }
 
  
  public void executeTestPage(PageWidget pageWidget, String pagePath) {
    executingListener = new PageExecutingListenerImpl(pageWidget);
    m_progressBar.setVisible(true);
	startIndicator = true;
    m_pageExecutingService.executePage(pagePath, new AsyncCallback<PageExecutingResult>() {

      @Override
      public void onSuccess(PageExecutingResult arg0) {
        PageExecutingState currentState = arg0.getState();
        if (currentState == PageExecutingState.FINISH) {
          executingListener.processPageExecutingResult(arg0);
	  m_progressBar.updateView(executingListener.getCounts());
          executingListener.finish();
        }
        if (currentState != PageExecutingState.FAIL) {
          final String id = arg0.getID();
          Timer timer = new Timer() {

            @Override
            public void run() {
              processExecutingPage(id);
            }
          };
          executingListener.setTimer(timer);
          timer.scheduleRepeating(1000);
        }
      }

      @Override
      public void onFailure(Throwable arg0) {

      }
    });

  }

  private void processExecutingPage(String id) {
    m_pageExecutingService.getExecutingResult(id, new AsyncCallback<PageExecutingResult>() {

      @Override
      public void onFailure(Throwable arg0) {

      }

      @Override
      public void onSuccess(PageExecutingResult arg0) {
        if (arg0 != null) {
          if (arg0.getState() == PageExecutingState.FINISH) {
            executingListener.stopTimer();
            executingListener.processPageExecutingResult(arg0);
        //    m_fitnesse.updateCounts(executingListener.getCounts());
	    m_progressBar.updateView(executingListener.getCounts());
            executingListener.finish();
          } else {
            executingListener.processPageExecutingResult(arg0);
	    m_progressBar.updateView(executingListener.getCounts());
         //   m_fitnesse.updateCounts(executingListener.getCounts());
          }
        } else {
          executingListener.stopTimer();
        }
      }
    });
  }

  public void setProgressBar(ProgressBar pBar) {
    m_progressBar = pBar;
  }
  
  
  public boolean testStarted() {
	return startIndicator;
  }
  
}
